<link href="__ROOT__/Public/Admin/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="__ROOT__/Public/Admin/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="__ROOT__/Public/Admin/css/animate.css" rel="stylesheet">
    <link href="__ROOT__/Public/Admin/css/style.css?v=4.1.0" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="__ROOT__/Public/Admin/js/wangEditor/css/wangEditor.min.css">
<link href="__ROOT__/Public/Admin/css/plugins/chosen/chosen.css" rel="stylesheet">
<style type="text/css">
  #GUID_chosen .search-field input{width: auto !important; }
</style>
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-10">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <form class="form-horizontal m-t js-ajax-forms" id="signupForm" action="{:U('Admin/Message/send_post')}" method="post" enctype="multipart/form-data">
                            <div class="form-group">
                                <label class="col-sm-3 control-label">标题 *</label>
                                <div class="col-sm-8">
                                    <input id="Title" name="Title" class="form-control" type="text" class="error">
                                </div>
                            </div>
                           
                            <div class="form-group">
                              <label class="col-sm-3 control-label">选择接收用户*</label>
                              <div class="col-sm-8">
                                  <select data-placeholder="选择接收用户" class="form-control chosen-select m-b" multiple style="width:70%;" tabindex="4" id="GUID" name="GUID[]">
                                      <option value="all">选择接收用户</option>
                                      
                                  </select>
                                 
                                  (按住ctrl多选)
                              </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">内容 *</label>
                                <div class="col-sm-8">
                                    <div id="Content" name="Content" style="height:350px;">
										                  <?php echo htmlspecialchars_decode($obj['content']);?>
									                  </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-8 col-sm-offset-3">
                                    <button class="btn btn-primary" type="submit" id="submit">发送</button>
                                    <a class="btn btn-white" href="javascript:history.back(-1);">返回</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>


 
    <!-- 全局js -->
    <script src="__ROOT__/Public/Admin/js/jquery.min.js?v=2.1.4"></script>
    <script src="__ROOT__/Public/Admin/js/bootstrap.min.js?v=3.3.6"></script>
    
  
    <script src="__ROOT__/Public/Admin/js/plugins/validate/jquery.validate.min.js"></script>
    <script src="__ROOT__/Public/Admin/js/plugins/validate/messages_zh.min.js"></script>
    <script src="__ROOT__/Public/Admin/js/wangEditor/js/wangEditor.min.js"></script>
    <script src="__ROOT__/Public/Admin/js/plugins/chosen/chosen.jquery.ajax.tip.js"></script>
    <script type="text/javascript">
  	  var icon = "<i class='fa fa-times-circle'></i> ";
  		var form = $('form.js-ajax-forms');
  		var formloading = false;
      var url_saveFrom = "__APP__/?m=Admin&c=Message&a=send_post";
  		var url_list = "__APP__/?m=Admin&c=Message&a=index";
      $('#GUID').chosen();  
      $('#chosen-all').click(function(){
          // $("#GUID option").each(function(i,n){
          //   $(n).attr("selected",'selected');
          // })
          // $('#GUID').trigger("chosen:updated");
          $('#GUID').html('<option value="all" selected="selected">全部用户</option>');
          $('#GUID').trigger("chosen:updated");
          $('#clean-user').click(function(){
            $('#GUID').html('');        
            $('#GUID').trigger("chosen:updated");
            $('#GUID_chosen .search-field input')[0].addEventListener('input',function(){
              var _this = this;
              setTimeout(function(){
                var keyword = _this.value
                chosenAjax(keyword)
              },500)
            })
          })
      })
      $('#GUID_chosen .search-field input')[0].addEventListener('input',function(){
        var _this = this;
        setTimeout(function(){
          var keyword = _this.value
          chosenAjax(keyword)
        },500)
      })

      function chosenAjax(keyword){
        if(keyword.length>0){
          $.ajax({   
            type:"POST",
            url:'__APP__?m=Admin&c=Message&a=chosen_user',
            data:{keyword:keyword},
            success:function(data){
              if (data.datas.length>0) {
                var resDatas = data.datas;
                var option = '';
                for(var i in resDatas){
                    var username =  resDatas[i].UserName;
                    if(resDatas[i].TrueName.length>1){
                      username=resDatas[i].TrueName;
                    }
                    option += '<option value="'+resDatas[i].ID+'">'+username+'</option>';
                }

                setTimeout(function(){
                  $('#GUID').html(option);
                  $('#GUID').trigger("chosen:updated");
                  $('#GUID_chosen .search-field input')[0].value = keyword;
                },500)
              }else{

                setTimeout(function(){
                  $('.no-results').html('没有结果"<span>'+keyword+'</span>"');
                },500)
              }
            }
          });
        }else{
          search_loading = false;
          $('#GUID').html('');
          $('#GUID').trigger("chosen:updated");
        }
      }
  		function submitForm(){
  			
  		  var param = {
             	"Title":$("#Title").val(),
             	"GUID":$("#GUID").val(),
             	"Content":$("#Content").html()
        };
        $('#submit').html('发送中……');
        $.ajax({   
   	        type:"POST",
   	        url:url_saveFrom,
   	        data:param,
   	        success:function(data){
   	        	if (data.code=="0") {
     	          alert('发送成功');
                document.location.reload();
     	        }else{
                alert('发送失败');
              }
   	        },
   	        beforeSend:function(){
   	        	formloading = true;
   	        },
   	        complete:function(){
   	        	formloading = false;
   	        }
   	    });
  		
  		}
  		
  		
	    $(document).ready(function () {
	    	var Content = new wangEditor('Content');
	    	Content.config.uploadImgUrl = "{:U('Admin/Message/up_msg_img_content')}";
			  Content.create();
	        //表单验证开始
	        form.validate({
	            //使用validate中的ignore忽略summernote上传文件的验证，summernote的写法是固定的，其它编辑器根据实际情况而定
	            ignore:".note-image-input,.note-group-select-from-files",
	            //验证规则
	            rules: {
	                Title: {
	                    required: true,
	                    maxlength: 90
	                },
	                GUID: {
	                    required: true
	                },
	                Content: {
	                    required: true
	                }
	            },
	            //验证未通过提示消息
	            messages: {
	                Title:{
	                    required: icon + "请输入标题",
	                    maxlength: icon + "标题最大长度90个字符"
	                },
	                GUID:{
	                	required: icon + "请选择用户"
	                },
	                Content: {
	                   required: icon + "请填写内容"
	                }
	            },            
	            submitHandler : function(forms) {
	                if (formloading) return;
	                submitForm();
	            }
	        });
	    });
    </script>
</body>
</html>